package org.jeecg.modules.online.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jeecg.modules.online.entity.DiagramCombination;
import org.jeecg.modules.online.mapper.DiagramCombinationMapper;
import org.jeecg.modules.online.service.IDiagramCombinationDetailService;
import org.jeecg.modules.online.service.IDiagramCombinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class DiagramCombinationServiceImpl extends ServiceImpl<DiagramCombinationMapper, DiagramCombination>
		implements IDiagramCombinationService {
	
	@Autowired
	private IDiagramCombinationDetailService diagramCombinationDetailService;
	
	@Resource
	private DiagramCombinationMapper diagramCombinationMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(DiagramCombination diagramCombination) {
		// 保存主表信息
		this.save(diagramCombination);
		// 保存子表信息
		if (diagramCombination != null && diagramCombination.getDiagramCombinationDetails() != null
				&& !diagramCombination.getDiagramCombinationDetails().isEmpty()) {
			diagramCombination.getDiagramCombinationDetails()
					.forEach(dcd -> dcd.setGraphreportTempletId(diagramCombination.getId()));
			//Integer.parseInt("hello");
			this.diagramCombinationDetailService.saveBatch(diagramCombination.getDiagramCombinationDetails());
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void edit(DiagramCombination diagramCombination) {
		this.updateById(diagramCombination);
		// 更新子表信息
		Map<String, Object> columnMap = new HashMap<>();
		columnMap.put("GRAPHREPORT_TEMPLET_ID", diagramCombination.getId());
		this.diagramCombinationDetailService.removeByMap(columnMap);
		this.diagramCombinationDetailService.saveBatch(diagramCombination.getDiagramCombinationDetails());

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteById(String id) {
		DiagramCombination diagramCombination = super.getById(id);
		this.removeById(id);
		// 删除子表信息
		Map<String, Object> columnMap = new HashMap<>();
		columnMap.put("GRAPHREPORT_TEMPLET_ID", diagramCombination.getId());
		this.diagramCombinationDetailService.removeByMap(columnMap);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(String ids) {
		List<String> idList = Arrays.asList(ids.split(","));
		QueryWrapper<DiagramCombination> queryWrapper = new QueryWrapper<>();
		queryWrapper.in("ID", idList);
		List<DiagramCombination> diagramCombinationList = super.list(queryWrapper);
		this.removeByIds(idList);
		// 删除子表信息
		if (diagramCombinationList != null && !diagramCombinationList.isEmpty()) {
			Map<String, Object> columnMap = new HashMap<>();
			for (DiagramCombination diagramCombination : diagramCombinationList) {
				columnMap.clear();
				columnMap.put("GRAPHREPORT_TEMPLET_ID", diagramCombination.getId());
				this.diagramCombinationDetailService.removeByMap(columnMap);
			}
		}
	}

	@Override
	public List<DiagramCombination> selectBySql(String sql) {
		return this.diagramCombinationMapper.selectBySql(sql);
	}

}
